<?php
namespace Home\Model;
use Think\Model;

class UserModel extends Model{


     public function info(&$id='')
    {
         
            $user=M('user')->find($id);
            $invite_id=M('user')->where('id='.$id)->getField('invite_id');

            if ($invite_id>0) {
              $invite_info=M('user')->find($invite_id);
              $up_invite_status=$invite_info['status'];
              $jb=M('jbset')->find();
              switch ($up_invite_status) {
                case '1':
                 $sjcon['status']=1;
                  $con_num=$jb['sj_jb2_jb1s'];
                  $add_sj['sj_time']=time();
                  $save_sj['sj_invite']=1;
                  $sj_ordernum='sj_ordernum';
                 
                  break;
                case '2':
                 $sjcon['status']=2;
                  $con_num=$jb['sj_jb3_jb2s'];
                   $add_sj['sj3_time']=time();
                   $save_sj['sj3_invite']=1;
                   $sj_ordernum='sj3_ordernum'; 
                   $each=10;
                  break;
                case '3':
                 $sjcon['status']=3;
                  $con_num=$jb['sj_jb4_jb3s'];
                   $add_sj['sj4_time']=time(); 
                   $save_sj['sj3_invite']=1;
                   $sj_ordernum='sj4_ordernum';
                   $each=20;
                  break;
                case '4':
                   $each=40;
                  break;
                
                default:
                  # code...
                  break;
              }
             $up_invite_num=M('user')->where('invite_id='.$invite_id)->where($sjcon)->count();
       
             if ($up_invite_num>=$con_num) {
               $sj=M('user')->where('id='.$invite_id)->save($save_sj);
               $sj_ordernum=M('user')->where('id='.$invite_id)->getField($sj_ordernum);

               if ($sj_ordernum) {
                  $sj_status=$up_invite_status>=4?$up_invite_status:$up_invite_status+1;

                     if ($sj_status>2) {
                    $add_sj['status']=$sj_status;
                    $add_sj['self_ztxf']=$invite_info['fh_ordernums']%10;
                    $add_sj['self_flordernum']=$invite_info['self_flordernum']%10;
                    $add_sj['fh_ordernums']=$invite_info['fh_ordernums']%$each;
                  }else{
                  $add_sj['status']=$sj_status;
                  $add_sj['self_ztxf']=0;
                  $add_sj['self_flordernum']=0;
                  $add_sj['fh_ordernums']=0;
                  }

                  
                 $res_sj=M('user')->where('id='.$invite_id)->save($add_sj);
                M('auth_group_access_home')->where("uid=$invite_id")->setField("group_id",$up_invite_status);
                 if ($res_sj) {
                      //判断是否平级超越
                  $up_up_id=M('user')->where('id='.$invite_id)->find();
                    if ($up_up_id['invite_id']>0) {
                          $up_up_id=$up_up_id['invite_id'];
                          $up_up_status=M('user')->where('id='.$up_up_id)->getField('status');
                        if ($add_sj['status']>=$up_up_status  ) {


                           if ($add_sj['status']==$up_up_status) {
                             M('user')->where('id='.$invite_id)->setField('f_id',0);
                          }else{
                             $beyond_months= M('jifen')->getField('beyond_months');
                          $beyond['beyond_end_time']= strtotime("+".$beyond_months."months");
                          $beyond['beyond_sj_id']=$up_up_id;
                          $beyond['beyond_time']=time();
                          $beyond['f_id']=-1;
                      M('user')->where("id=$invite_id")->save($beyond);
                        $c=M('user')->where("invite_id=$invite_id")->select();
                        foreach ($c as $key => $value) {
                           if ($value['status']<$add_sj['status']) {
                                 M('user')->where('id='.$value['id'])->setField('f_id',$value['invite_id']);
                            } 
                        }


                          }

                         

                  }
                    }
                      
             

                 }

               }
               
             } 
              $this->info($invite_id);  
            }    

           
        
    }



     public function set_xj_userstatus($userid){

        $userinfo = M('user')->find($userid);
        $save_sj['id']=$userid;
        $up_id=$userinfo['invite_id'];
        $up_userinfo = M('user')->find($up_id);
        $up_status=$up_userinfo['status'];
        $jb=M('jbset')->find();
         $con['invite_id']=$userinfo['id'];
          switch ($userinfo['status']) {

                    case '1':
                       $con['status']=1;
                       $yq_num=$this->where($con)->count();
                       if ($yq_num>= $jb['sj_jb2_jb1s']) {
                           $save_sj['sj_invite']=1;
                           $this->save($save_sj);
                       }

                        break;
                    case '2':

                    $con['status']=2;
                       $yq_num=$this->where($con)->count();
                       if ($yq_num>= $jb['sj_jb3_jb2s']) {
                           $save_sj['sj3_invite']=1;
                           $this->save($save_sj);
                       }
                        break;
                    case '3':

                    $con['status']=3;
                       $yq_num=$this->where($con)->count();
                       if ($yq_num>= $jb['sj_jb4_jb3s']) {
                           $save_sj['sj4_invite']=1;
                           $this->save($save_sj);
                       }
                        break;
                    
                    default:

            } 
     


     }
}
